<?php
namespace Home\Controller;
use Org\Com\Message;
class LinuxController extends PublicController
{
    const status_ok=1;
    const status_no=0;
    const pagecount=10;	
	
	
	
	/**
	 * 权限设置
	 */
	public function right(){
	    $right=$this->getright();
	    $list=M("admin")->field("uid,username")->where(array("status"=>1,"uid"=>array("neq",1)))->select();
	    $this->assign("right",$right);
	    $this->assign("class",$list);
	    $this->display();
	}
	
	public  function getright(){
	    if(!S("right")){
	        $list=M("bigcategroy")->select();
	        if(is_array($list)){
	            $atty=array();
	            $op=M("smallcategroy");
	            foreach ($list as $key=>$value){
	                $atty[$key]["id"]=$value["id"];
	                $atty[$key]["bigcategroyname"]=$value["bigcategroyname"];
	                $atty[$key]["bigaction"]=$value["bigaction"];
	                $mylist=$op->where(array("parentid"=>$value["id"]))->select();
	                if(is_array($mylist)){
	                    $temp=array();
	                    $myop=0;
	                    foreach ($mylist as $myvalue){
	                        $temp[$myop]["smallcategroy"]=$myvalue["smallcategroy"];
	                        $temp[$myop]["smallaction"]=$myvalue["smallaction"];
	                        $myop++;
	                    }
	                    $atty[$key]["smallinfo"]=$temp;
	                }
	            }
	            if(!S("right")){
	                S("right",$atty);
	            }
	            return  $atty;
	        }
	    }else{
	        return S("right");
	    }
	}
	/**
	 * 保存设置的权限
	 */
	public function setright(){
	     
	    if(IS_POST){
	        $po = $_POST;
	        foreach($po as $key=>$val){
	            if(strstr($key,'bigcate')){
	                $bigcate[]=$val;
	            }elseif(strstr($key,'smallcate')){
	                $smallcate[]=$val;
	            }
	             
	        }
	        $uid=$po['userid'];
	        $data['uid']=$po['userid'];
	        $data['bigcate']=implode(',',$bigcate);
	        $data['smallcate']=implode(',',$smallcate);
	        if(M("power")->where(array('uid'=>$uid))->find()){
	            $mm = M("power")->where(array('uid'=>$uid))->save($data);
	        }else{
	            $mm = M("power")->where(array('uid'=>$uid))->add($data);
	        }
	        if($mm){
	            $this->success("提交成功！");
	        }else{
	            $this->error("提交失败！");
	        }
	         
	    }
	     
	}
	
	/**
	 * 通过ajax显示权限
	 */
	public function showright(){
	    if(IS_POST){
	        $id=I("post.uid");
	        $power=M("power")->field("bigcate,smallcate")->where(array("uid"=>intval($id)))->find();
	        if($power){
	            $data["bigcate"]=explode(",", $power["bigcate"]);
	            $data["smallcate"]=explode(",", $power["smallcate"]);
	            Message::Outmessage(0, 1, $data,"查询成功");
	        }else{
	            Message::Outofmessagenodata(1, "此用户没有权限！");
	        }
	    }
	}
}